﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HsTec.DigitalPlatform.DataAccess
{
    public class ItemInfoRepository
    {
        DigitalPlatformEntities db = new DigitalPlatformEntities();

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="iteminfo"></param>
        public void Add(ItemInfo iteminfo)
        {
            db.AddToItemInfos(iteminfo);
            db.SaveChanges();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="iteminfo"></param>
        public void Delete(ItemInfo iteminfo)
        {
            db.DeleteObject(iteminfo);
            db.SaveChanges();
        }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            db.SaveChanges();
        }

        /// <summary>
        /// 根据ID查询实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ItemInfo GetItemInfoByID(int id)
        {
            return db.ItemInfos.FirstOrDefault<ItemInfo>(d => d.itemid == id);
        }

        /// <summary>
        /// 返回所有实体
        /// </summary>
        /// <returns></returns>
        public IQueryable<ItemInfo> FindALLItemInfo()
        {
            return db.ItemInfos;
        }

        public IQueryable<ItemInfo> PageInfo(int page, int rp, string sortname, string sortorder)
        {
            return db.ItemInfos.OrderBy("it." + sortname + " " + sortorder).Skip((page - 1) * rp).Take(rp);
        }

        public int GetAllCount()
        {
            return db.ItemInfos.Count();
        }

        public IQueryable FindItemInfoByType(string itemtype, string sex)
        {
            return from c in db.ItemInfos
                   where c.itemtype == itemtype && c.itemsex.Contains(sex)
                   select new
                   {
                       id = c.itemid,
                       name = c.itemname
                   };
        }
    }
}
